/**
* 
* Sonde system specific task sonde 
* Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN  
* 
* see <a href="{@docRoot}/Documentation/sonde.html"> for more information</a>
* 
* @author: Urunuela Richard
* @version 3.2
*/
package report;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import storm.Schedulers.Scheduler;
import storm.Tasks.Task;
import storm.Tasks.Task.Activation;

public class tasks extends sonde {
	ArrayList listeTask;
	/**
	 * 
	 * @param theOutPut
	 * @param arrayList
	 */
	public tasks(output theOutPut, ArrayList arrayList) {
		this.theOutPut = theOutPut;
		this.listeTask = arrayList;
		this.theOutPut.appendSection("Tasks");
	}
	/**
	 * 
	 */
	public void internalData(String commande) {
		/**
		 *		 proper information 
		 */

		if (commande.compareToIgnoreCase("info")==0){
			Vector info = new Vector();
			this.theOutPut.addligne("");
			info.add("Name");
			info.add("Type");
			info.add("Period");
			info.add("WCET");
			info.add("BCET");
			info.add("priority");
			info.add("AET Rule");
			
			this.theOutPut.add(info);
			
			Iterator i  = this.listeTask.iterator();
			while(i.hasNext()){
				Task crt = (Task) i.next();
				
				Vector info2 = new Vector();
				
				info2.add(crt.getName());
				info2.add(crt.getClass()+"");
				info2.add(crt.getPeriod());
				info2.add(crt.getWCET());
				info2.add(crt.getBCET());
				info2.add(crt.getPriority());
				info2.add(crt.getStringAETRule());
				
				this.theOutPut.add(info2);
				
				
			}
			
		}
		/**
		 * number of miss deadline
		 */
		if (commande.compareToIgnoreCase("nbrlost")==0){
			int res = 0;
			Iterator i  = this.listeTask.iterator();
	
			while(i.hasNext()){
				Task crt = (Task) i.next();
				
				java.util.LinkedList A = crt.getActivation();
				Iterator j = A.iterator() ;
		
				while (j.hasNext()){
					Activation Act= (Activation) j.next();
					if (Act.isLoseActivation()) {
						res++;  
					}
					
				}
			}
			
			
			this.theOutPut.addligne("Total lost: "  + res);
		}
		if (commande.compareToIgnoreCase("nbrmiss")==0){
			int res = 0;
			Iterator i  = this.listeTask.iterator();
	
			while(i.hasNext()){
				Task crt = (Task) i.next();
				
				java.util.LinkedList A = crt.getActivation();
				Iterator j = A.iterator() ;
		
				while (j.hasNext()){
					Activation Act= (Activation) j.next();
					//System.out.println(Act.getDeb_exec() + " -- " + Act.getEnd_exec() +" - - "+ Act.getDeadline());
					if (Act.isMissed()) {
						res++;  
					}
					
				}
			}
		
			this.theOutPut.addligne("Total Miss: "  + res);
		
		}
		if (commande.compareToIgnoreCase("miss")==0){
			this.theOutPut.addligne("");
			this.theOutPut.beginTab("Miss deadline",4);

			Iterator i  = this.listeTask.iterator();
			while(i.hasNext()){
				Task crt = (Task) i.next();
				this.theOutPut.beginTab("Task "+ crt.getName(),4);
				java.util.LinkedList A = crt.getActivation();
				Iterator j = A.iterator() ;
				Vector v2 = new Vector();
				v2.add("ID");
				v2.add("Date");
				v2.add("Begin ");
				v2.add("Deadline ");
				v2.add("End");
				this.theOutPut.add(v2);
				while (j.hasNext()){
					Activation Act= (Activation) j.next();
					if (Act.isMissed()) {
						Vector v = new Vector();
						v.add(crt.getId()+"");
						v.add(Act.getDate()+"");
						v.add(Act.getDeb_exec()+"");
						v.add(Act.getDeadline()+"");
						v.add(Act.getEnd_exec()+"");
						this.theOutPut.add(v);
						//this.theOutPut.addligne(crt.getId()+ " missed " + Act.getDate() + " at " + Act.getDeadline() + " ended at " + Act.getEnd_exec());  
					}
					
				}
			}
			this.theOutPut.EndTab("Miss deadline");
		
		}
		if (commande.compareToIgnoreCase("abort")==0){
			this.theOutPut.addligne("");
			this.theOutPut.beginTab("Abort execution",4);
			Iterator i  = this.listeTask.iterator();
			while(i.hasNext()){
				Task crt = (Task) i.next();
				this.theOutPut.beginTab(crt.getName(),4);
				Vector v2 = new Vector();
				v2.add("ID");
				v2.add("Date");
				
				v2.add("Deadline ");
				
				this.theOutPut.add(v2);
				java.util.LinkedList Activation = crt.getActivation();
				Iterator j = Activation.iterator() ;
				while (j.hasNext()){
					Activation Act= (Activation) j.next();
					if (Act.isAbort()) {
						Vector v = new Vector();
						v.add(crt.getId()+"");
						v.add(Act.getDate()+"");
						
						v.add(Act.getDeadline()+"");
						
						this.theOutPut.add(v);
						//this.theOutPut.addligne(crt.getId()+" abort " + Act.getDate() + " at " + Act.getDeadline());  
					}
					
				}
			}
		
		}
		if (commande.compareToIgnoreCase("lostAct")==0){
			this.theOutPut.addligne("");
			this.theOutPut.beginTab("Lose execution",4);
			Iterator i  = this.listeTask.iterator();
			while(i.hasNext()){
				Task crt = (Task) i.next();
				this.theOutPut.beginTab(crt.getName(),4);
				java.util.LinkedList Activation = crt.getActivation();
				Iterator j = Activation.iterator() ;
				Vector v2 = new Vector();
				v2.add("ID");
				v2.add("Lose activation date");
				
				this.theOutPut.add(v2);
				
				while (j.hasNext()){
					Activation Act= (Activation) j.next();
					if (Act.isLoseActivation()) {
						Vector v = new Vector();
						v.add(crt.getId()+"");
						v.add(Act.getDate()+"");
						
						
						
						this.theOutPut.add(v);
						//this.theOutPut.addligne(crt.getId()+" loseActivation " + Act.getDate() );  
					}
					
				}
			}
		
		}
	}

	//attribut ajouter dans specialisation
	

}
